/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.DHTMLConstants;
import model.Labels;
import model.Vocabulary;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.ClassMapper;
import controller.elementMapper.Mapper;


public class PropertiesHavingRangeClass extends AbstractComponent {
	

	private static final String CSSIDFIELD = "divpropertieshavingclassinrange";
	public static final String CSSIDCONTENT = "divpropertieshavingclassinrangecontent";
	public static final String NAMEFORMELEMENT = "propertiesHavingClassInRangeEntry";
	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		String val = null;
		if( uri!=null ) {
			val = StringEscapeUtils.escapeHtml( Mapper.selectLabel( uri, Vocabulary.ENGLISH ) );
		}
		if( val==null )
			val = "this " + Labels.CLASS.getNameSingular();

		ret += getTitleContainer( Labels.PROPERTY.getNamePlural() + " having " + val + " in " + Labels.RANGE.getNameSingular(), DHTMLConstants.PROPERTY_VIEWTYPE );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode( URI uri, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		String ret = "";
		
		String val = null;
		if( uri!=null ) {
			val = Mapper.selectLabel( uri, Vocabulary.ENGLISH);
		}
		if( val==null )
			val = "this " + Labels.CLASS.getNameSingular();

		ret += getTitleContainer( Labels.PROPERTY.getNamePlural() + " having " + val + " in " + Labels.RANGE.getNameSingular(), DHTMLConstants.PROPERTY_VIEWTYPE);
		ret += getFormContainer( getFormInEditMode( uri, entryList, exceptions, showNavigation, showCreateLinks ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";	

		java.util.ArrayList<URI> list = ClassMapper.selectAllPropertiesHavingClassInRange( uri );		
		
		String entry = HelperFunctions.concatURIs( list );
		String content = getElementsHTMLList( list );	

		ret = renderViewForm( content, entry, CSSIDCONTENT, showNavigation, showNavigationInEditMode );
		
		return ret;
	}
		

	
	public static String getFormInEditMode( URI uri, ArrayList<URI> entryList, ArrayList<Exception> exceptions, boolean showNavigation, boolean showCreateLinks ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualVersionEntry = "";
		
		if( uri!=null ) {

			ArrayList<URI> tmp = ClassMapper.selectAllPropertiesHavingClassInRange( uri );
			
			if( tmp!=null ) {
				actualVersionEntry = HelperFunctions.concatURIs( tmp );
				actualVersionContent = getElementsHTMLList( tmp );
			}
		}
		
		// String content = getSelectBoxReduceableByOM( NAMEFORMELEMENT, entry, DHTMLConstants.CLASSESBELONGINGTOOM );

		String createNewAdditionalParams = Range.NAMEFORMELEMENT + "=" + HelperFunctions.getUriForHref( uri );
		
		String content = getItemsEditPanel( NAMEFORMELEMENT, entryList, DHTMLConstants.PROPERTY_VIEWTYPE, createNewAdditionalParams, showCreateLinks );
		
		ret = renderEditForm( 
				null, 
				content,
				exceptions, 
				actualVersionContent, 
				actualVersionEntry,
				CSSIDCONTENT, 
				NAMEFORMELEMENT, 
				showNavigation );


		return ret;
	}	

}
